/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdiaTarefasView.java
 *
 * Created on 30/08/2010, 11:29:18
 */

package view.execucao;

import Util.FabricaConexoes;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import model.execucao.Transicao;

/**
 *
 * @author Administrador
 */
public class AdiaTarefasView extends javax.swing.JDialog {

    /** Creates new form AdiaTarefasView */
    public AdiaTarefasView(java.awt.Frame parent, boolean modal, Transicao trans) {
        super(parent, modal);
        initComponents();
        this.trans = trans;
        dt_vencimento.setDate(trans.getDt_limite());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dt_vencimento = new net.sf.nachocalendar.components.DatePanel();
        jl_vencimento = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jbt_ok = new javax.swing.JButton();
        jbt_cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jl_vencimento.setText("Nova data para vencimento:");

        jbt_ok.setText("Gravar");
        jbt_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_okActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_ok);

        jbt_cancelar.setText("Cancelar");
        jbt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbt_cancelarActionPerformed(evt);
            }
        });
        jPanel1.add(jbt_cancelar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jl_vencimento)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dt_vencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jl_vencimento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dt_vencimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbt_cancelarActionPerformed

    private void jbt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbt_okActionPerformed
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dt_vencimento.getDate());
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(trans.getDt_limite());
            DateFormat dt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            PreparedStatement stm;
            if(calendar.compareTo(calendar2) < 1)
            {
                JOptionPane.showMessageDialog(null, "A data de vencimento não pode ser menor ou igual a data limite.","Erro",0);
            }else
            {
                try{
                String sql = "update transicao set dt_limite = ?, status = 0 where idtarefa = ?";
                if (!FabricaConexoes.verificaConexao()) {
                    FabricaConexoes.getConexao();
                }
                stm = FabricaConexoes.returnStatement(sql);
                Date data = calendar.getTime();
                stm.setString(1,dt.format(data));
                stm.setInt(2, trans.getIdtarefa());
                stm.execute();
                stm.close();
                dispose();
                }catch(SQLException exc)
                {
                    JOptionPane.showMessageDialog(null, exc.getMessage(),"Erro",0);
                }
            }
    }//GEN-LAST:event_jbt_okActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AdiaTarefasView dialog = new AdiaTarefasView(new javax.swing.JFrame(), true, new Transicao());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private net.sf.nachocalendar.components.DatePanel dt_vencimento;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbt_cancelar;
    private javax.swing.JButton jbt_ok;
    private javax.swing.JLabel jl_vencimento;
    // End of variables declaration//GEN-END:variables
   Transicao trans;
}
